
#include<iostream>
#include<string.h>
#include<strings.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include <arpa/inet.h>

#include"Log.hpp"
using namespace LogMod;

int main(int argc,char * argv[])
{
    if(argc != 3)
    {
        LOG(level::ERROR)<<"you should"<<argv[0]<<" ip port";
    }
    int port = std::stoi(argv[2]);
    char* ip = argv[1];
    int _sfd = socket(AF_INET,SOCK_DGRAM,0);
    while(1)
    {
        struct sockaddr_in addr;
        bzero(&addr,0);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(ip);
        socklen_t size = sizeof(addr);
        std::string strsay;
        std::cout<<"please say# ";
        //getline(strsay,sizeof(strsay),stdin);
        std::getline(std::cin,strsay);
        ssize_t sz = sendto(_sfd,strsay.c_str(),strsay.size(),0,(struct sockaddr*)&addr,size);
        
        char buff[512];
        struct sockaddr_in revaddr;
        bzero(&revaddr,0);
        socklen_t revsize = sizeof(revaddr);
        ssize_t n = recvfrom(_sfd,buff,sizeof(buff),0,(struct sockaddr*)&revaddr,&revsize);
        if(n > 0)
        {
            buff[n] = 0;
            std::cout<<buff<<std::endl;
        }
    }
    return 0;
}